package com.jic.market.config;

import com.google.common.collect.ImmutableMap;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.Map;

@Configuration
@EnableCaching
public class CacheConfig {
	Map<String, String> CAFFEINE_CONFIG_MAP =
			new ImmutableMap.Builder<String, String>()
					.put("s30", "maximumSize=5000,expireAfterWrite=30s")
					.put("m1", "maximumSize=5000,expireAfterWrite=1m")
					.put("m10", "maximumSize=5000,expireAfterWrite=10m")
					.put("1h", "maximumSize=5000,expireAfterWrite=1h")
					.build();
	/**
	 * 创建基于Caffeine的Cache Manager
	 * @return
	 */
	@Bean
	@Primary
	public CacheManager caffeineCacheManager() {
		FlexibleCaffeineCacheManager cacheManager = new FlexibleCaffeineCacheManager();
		cacheManager.setCacheSpecification("maximumSize=500,expireAfterWrite=30m");
		cacheManager.setCacheSpecs(CAFFEINE_CONFIG_MAP);
		return cacheManager;
	}
	
}
